FROM nvidia/cuda:11.6.2-cudnn8-devel-ubuntu20.04

ENV PATH="/usr/local/cuda/bin:${PATH}"
ENV LD_LIBRARY_PATH="/usr/lib/x86_64-linux-gnu:/usr/local/nvidia/lib64:/usr/local/nvidia/lib:/usr/local/cuda/lib64:/usr/local/cuda/lib:${LD_LIBRARY_PATH}"
ENV TZ=Asia/Shanghai
ENV LANG=C.UTF-8
ENV LC_ALL=C.UTF-8

ARG USER=v-xz21
ARG USER_ID=10120
ARG GROUP=labmen
ARG GROUP_ID=10000
ARG SSH_PORT=22
USER root
ENV http_proxy=""
ENV https_proxy=""
RUN printf "\
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse \n\
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse \n\
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse \n\
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse \n\
    " > /etc/apt/sources.list && \
    ( rm /etc/apt/sources.list.d/nvidia-ml.list || echo "nvidia-ml.list not exist" ) && \
    ( rm /etc/apt/sources.list.d/cuda.list || echo "cuda.list not exist" ) && \
    DEBIAN_FRONTEND=noninteractive apt-get -y update && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends ca-certificates &&\
    sed -i "s/http:\/\/mirrors\.tuna\.tsinghua\.edu\.cn\/ubuntu\//https:\/\/mirrors.tuna.tsinghua.edu.cn\/ubuntu\//g" /etc/apt/sources.list && \
    DEBIAN_FRONTEND=noninteractive apt-get -y update && \
    cat /etc/apt/sources.list && echo ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \
    DEBIAN_FRONTEND=noninteractive apt-get -y dist-upgrade && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        ca-certificates lsb-release iputils-ping \
        apt-utils apt-transport-https gnupg dirmngr openssl software-properties-common  \
        tar wget git mercurial vim openssh-client psmisc rsync \
        build-essential autoconf libtool \
        libssl-dev zlib1g-dev libncurses5-dev libncursesw5-dev libreadline-dev libsqlite3-dev \
        libcurl4-nss-dev libsasl2-dev libsasl2-modules libapr1-dev libsvn-dev \
        python-dev python-pip-whl graphviz curl sudo\
    && \
    DEBIAN_FRONTEND=noninteractive apt-get -y update && \
    DEBIAN_FRONTEND=noninteractive apt-get -y install nodejs && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

RUN (groupadd -g ${GROUP_ID} ${GROUP} || echo group ${GROUP} exists) && \
    (useradd -g ${GROUP} -u ${USER_ID} ${USER} || echo user ${USER} exists) && \
    ((mkdir /home/${USER} && chown -R ${USER}:${GROUP} /home/${USER}) || echo home dir exists) &&\
    echo "${USER}		ALL = (ALL) NOPASSWD: ALL" >> /etc/sudoers

USER ${USER}

## Install R and R requirements
RUN echo 'options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))' >> ~/.Rprofile && \
    sudo apt-get update -qq && \
    sudo apt-get install --no-install-recommends -y software-properties-common dirmngr && \
    sudo apt-get update && \
    sudo apt-get install -y --no-install-recommends \
    r-base-dev build-essential gfortran libgsl-dev libgit2-dev \
    libxml2-dev libharfbuzz-dev libfribidi-dev vim python3-pip htop lsof python3-dev

## Install ssh
RUN sudo apt-get install -y openssh-server

# Support mlrun
RUN echo "Port ${SSH_PORT}" | sudo tee -a /etc/ssh/sshd_config

# authorize SSH connection with root account
RUN sudo service ssh restart

USER root

# Install python requirements
RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U && \
    pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

RUN pip3 install jupyterlab --user
RUN echo "root:docker"|chpasswd
RUN echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
RUN service ssh restart

# Install Miniconda
RUN cd ~ && \
    wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh && \
    bash Miniconda3-latest-Linux-x86_64.sh -b && \
    /root/miniconda3/bin/conda config --set auto_activate_base false && \
    /root/miniconda3/bin/conda init && \
    rm Miniconda3-latest-Linux-x86_64.sh

RUN cd ~ && echo 'channels: \n\
  - defaults \n\
show_channel_urls: true \n\
default_channels: \n\
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main \n\
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r \n\
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2 \n\
custom_channels: \n\
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud \n\
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud \n\
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud \n\
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud \n\
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud \n\
  pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud \n\
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud \n'\
>> .condarc
